<?php

namespace app\data\controller\shop;

use app\data\service\ExpressService;
use app\data\service\GoodsService;
use app\data\service\OrderService;
use app\data\service\PropertyService;
use app\data\service\UserUpgradeService;
use think\admin\Controller;
use think\admin\extend\CodeExtend;

/**
 * 资产管理
 * Class Property
 * @package app\data\controller\property
 */
class Property extends Controller
{
    /**
     * 绑定数据表
     * @var string
     */
    private $table = 'ShopProperty';

    /**
     * 控制器初始化
     */
    protected function initialize()
    {

    }

    /**
     * 资产管理
     * @auth true
     * @menu true
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function index()
    {
        $this->title = '资产管理';
        $query = $this->_query($this->table);
        $query->equal('status')
            ->where('deleted', 0)
            ->like('name#name')
            ->dateBetween('date')
            ->order('sort desc,id desc')
            ->page();
    }

    /**
     * 数据列表处理
     * @param array $data
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    protected function _page_filter(array &$data)
    {
    }

    /**
     * 添加债权数据
     * @auth true
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function add()
    {
        $this->mode = 'add';
        $this->title = '添加债权数据';
        $this->_form($this->table, 'form');
    }

    /**
     * 编辑债权数据
     * @auth true
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function edit()
    {
        $this->mode = 'edit';
        $this->title = '编辑债权数据';
        $this->_form($this->table, 'form');
    }

    /**
     * 匹配投资
     * @auth true
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function match(){
        $this->title = '匹配投资';
        $this->_form($this->table, 'form_match');
    }

    /**
     * 表单数据处理
     * @param array $data
     */
    protected function _match_form_filter(&$data)
    {
        if ($this->request->isPost()){

            if (input('action') == 'del'){
                $res = PropertyService::instance()->cancelProperty(input('id'));

                if ($res){
                    $this->success('解抵成功');
                }else{
                    $this->error('解抵失败');
                }
            }
        }else{
            $this->order_property = PropertyService::instance()->getOrderProperty(['property_id' => input('id')]);
        }
    }

    /**
     * 释放债权
     * @auth true
     */
    public function freed()
    {
        if ($this->request->isPost()){
            $property_id = input('id');

            $res = PropertyService::instance()->cancelAllByPropertyId($property_id);

            if ($res){
                $this->success('该债权匹配的所有投资释放成功');
            }else{
                $this->error('释放失败');
            }
        }
    }

    /**
     * 表单数据处理
     * @param array $data
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    protected function _form_filter(array &$data)
    {
        if ($this->request->isGet()) {
            if (!empty($data['img'])){
                $this->data = $data['img'];
            }

            if (isset($data['id'])){
                $this->order_property = PropertyService::instance()->getOrderProperty(['property_id' => $data['id']]);
            }
        } elseif ($this->request->isPost()) {
            if (isset($data['img'])){
                $imgs = [];
                foreach ($data['img'] as $k => $v){
                    if (empty($v)) continue;
                    $imgs[]['img'] =  $v;
                }
                $data['img'] = json_encode($imgs);
            }
        }
    }

    /**
     * 表单结果处理
     * @param boolean $result
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    protected function _form_result(bool $result)
    {
        if ($result && $this->request->isPost()) {
            $this->success('债权编辑成功！', 'javascript:history.back()');
        }
    }

    /**
     * 债权操作日志列表
     * @auth true
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function log()
    {
        $this->_query('shop_property_history')->like('property_name#property_name')->order('create_at desc,id desc')->page();
    }

    protected function _log_page_filter(&$data)
    {

        foreach ($data as $key => $item){
            $data[$key]['order_info'] = json_decode($item['order_info'],true);
            $data[$key]['property_info'] = json_decode($item['property_info'], true);
        }

    }

    /**
     * 删除债权数据
     * @auth true
     * @throws \think\db\exception\DbException
     */
    public function remove()
    {

        $id = input('id');

        $ex = PropertyService::instance()->getOrderProperty(['property_id' => $id, 'status' => 1]);

        if ($ex){
            return $this->error('该笔债权正在匹配中，请解抵后删除！');
        }


        $this->_save($this->table, $this->_vali([
            'deleted.in:0,1'  => '状态值范围异常！',
            'deleted.require' => '状态值不能为空！',
        ]));
    }

}